package com.es.disped.web.controller.course;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.course.CourseService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Course;
import com.es.disped.dao.model.CourseExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value="课程管理")
@Controller
@RequestMapping("/manage/course")
public class CourseController extends BaseController{
	
	private final String COURSE_NAMESPACE="com.es.disped.dao.mapper.course.CourseCustomMapper";

	@Autowired
	CourseService courseService;
	
	@ApiOperation(value="课程新增",response=MsgModel.class,httpMethod="POST",notes="系统管理员添加课程")
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="course", value="课程对象",required=true) Course course)
	{
		int count=0;
		if(Optional.of(course)
				.filter(name->course.getCourseName()!=null)
				.isPresent())
		{
			course.setCourseId(this.getUUID());
			course.setCourseUpdateDate(this.currentDate("yyyy-MM-dd HH:mm:ss"));
			course.setCourseValid(RecordStatus.USEAGE.getValue());
			count=courseService.insertSelective(course);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"课程保存成功":"课程保存失败",count==1?course:"");
	}
	
	
	@ApiOperation(value="课程更新",response=MsgModel.class,httpMethod="POST",notes="系统管理员更新课程")
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="course", value="课程对象",required=true) Course course)
	{
		int count=0;
		if(Optional.of(course)
				.filter(name->course.getCourseId()!=null)
				.isPresent())
		{
			count=courseService.updateByPrimaryKey(course);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"课程更新成功":"课程更新失败",count==1?course:"");
	}
	
	@ApiOperation(value="课程删除",response=MsgModel.class,httpMethod="POST",notes="系统管理员删除课程")
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="course", value="课程对象",required=true) Course course)
	{
		int count=0;
		if(Optional.of(course)
				.filter(name->course.getCourseId()!=null)
				.isPresent())
		{
			//删除对象及引用对象
			count=this.getService().delete(COURSE_NAMESPACE+".deleteCourseModel", course.getCourseId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"课程删除成功":"课程删除失败",count>=1?course:"");
	}
	
	@ApiOperation(value="课程检索",response=MsgModel.class,httpMethod="POST",notes="检索课程,包含模糊检索(课程名称)，精确检索（课程ID）")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="course", value="课程对象",required=false) Course course)
	{
		CourseExample listExample=new CourseExample();
		if(!this.isNull(course.getCourseId()))
		{
			listExample.createCriteria().andCourseIdEqualTo(course.getCourseId());
		}
		if(!this.isNull(course.getCourseName()))
		{
			listExample.createCriteria().andCourseNameLike("%"+course.getCourseName()+"%");
		}
		List<Course> courses=courseService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(courses)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(courses)?"课程加载成功":"课程加载失败",!this.isNull(courses)?courses:"");
	}
	
	
	@ApiOperation(value="课程分页加载",response=MsgModel.class,httpMethod="POST",notes="分页加载课程，分页参数[offset,limit]")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		CourseExample listExample=new CourseExample();
		PageModel<Course> pageModel=null;
		if(Optional.of(paramMap)
		.filter(offset->paramMap.containsKey("offset")!=false)
		.filter(limit->paramMap.containsKey("limit")!=false)
		.isPresent())
		{
			pageModel=courseService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"课程加载成功":"课程加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
